/*
 * @lc app=leetcode.cn id=206 lang=cpp
 *
 * [206] 反转链表
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
   public:
    ListNode* reverseList(ListNode* head) {
        ListNode* node = head;      // 记录当前节点
        ListNode* last = nullptr;   // 记录前驱节点
        ListNode* next = nullptr;   // 记录后驱节点
        while (node != nullptr) {
            next = node->next;
            node->next = last;
            last = node;
            node = next;
        }
        return last;
    }
};
// @lc code=end
